var myScroll,
    pullDownEl,
    pullDownOffset,
    pullUpEl,
    pullUpOffset;

/**
 * 初始化iScroll控件
 */
function initScroll() {

    pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');
    pullUpOffset = pullUpEl.offsetHeight;

    var scrollWrapEl = document.querySelector('.scroll-wrap');
    var windowH = $(document).height();
    var lineTopH = $('.line-top').height()
    scrollWrapEl.style.height = windowH - lineTopH + 'px'


    myScroll = new iScroll('wrapper', {
        scrollbarClass: 'myScrollbar', /* 自定义滚动条的样式名 */
        useTransition: true, /* 是否使用CSS变换*/
        topOffset: 40,
        scrollbars: true,
        onRefresh: function () {
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                if (none_data == 0) {
                    pullDownEl.querySelector('.pullDownLabel').innerHTML = '更新完毕！';
                } else {
                    if (pullDownEl.querySelector('.pullDownIcon').classList.contains('loading-show')) {
                        pullDownEl.querySelector('.pullDownIcon').classList.remove('loading-show');
                    }
                    pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新';
                }
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';

                if (none_data == 0) {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '已没有更多数据';
                } else {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
                }

            }
        },
        onScrollMove: function () { // 内容移动的回调
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                if (none_data == 0) {
                    if (pullDownEl.querySelector('.pullDownIcon').classList.contains('loading-show')) {
                        pullDownEl.querySelector('.pullDownIcon').classList.remove('loading-show');
                    }
                    pullDownEl.querySelector('.pullDownLabel').innerHTML = '已更新';
                } else {
                    if (pullDownEl.querySelector('.pullDownIcon').classList.contains('loading-show')) {
                        pullDownEl.querySelector('.pullDownIcon').classList.remove('loading-show');
                    }

                    pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新';
                }

                this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                if (pullDownEl.querySelector('.pullDownIcon').classList.contains('loading-show')) {
                    pullDownEl.querySelector('.pullDownIcon').classList.remove('loading-show');
                }
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新';
                this.minScrollY = -50;

            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';

                if (none_data == 0) {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '已没有更多数据';
                    return false;
                } else {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新';
                }

                this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 30) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
                this.maxScrollY = 500;
            }
        },
        onScrollEnd: function () {  //在滚动完成后的回调

            if (none_data == 0) {  //st_num==0当状态为0时候不在执行更新，并提示。
                if (pullDownEl.className.match('flip')) { // 下拉
                    pullDownEl.className = 'loading';
                    if (pullDownEl.querySelector('.pullDownIcon').classList.contains('loading-show')) {
                        pullDownEl.querySelector('.pullDownIcon').classList.remove('loading-show');
                    }
                    pullDownEl.querySelector('.pullDownLabel').innerHTML = '已更新';
                    pullDownAction(function () {
                        setTimeout(function () {
                            myScroll.refresh()
                            console.log('执行了-myScroll.refresh')
                        }, 20)
                    });
                    this.maxScrollY = 500;
                } else if (pullUpEl.className.match('flip')) { // 上拉
                    pullUpEl.className = 'loading';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '已没有更多数据';
                    pullUpAction(function () {
                        setTimeout(function () {
                            myScroll.refresh()
                        }, 20)
                    });
                    this.maxScrollY = 500;
                }
            } else {
                if (pullDownEl.className.match('flip')) { // 下拉
                    pullDownEl.className = 'loading';
                    pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中';
                    pullDownEl.querySelector('.pullDownIcon').classList.add('loading-show');
                    pullDownAction(function () {
                        pullDownEl.querySelector('.pullDownIcon').classList.remove('loading-show');
                        setTimeout(function () {
                            myScroll.refresh()
                        }, 20)
                    });
                } else if (pullUpEl.className.match('flip')) { // 上拉
                    pullUpEl.className = 'loading';
                    pullUpEl.querySelector('.pullUpIcon').classList.add('loading-show');
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中';
                    pullUpAction(function () {
                        pullUpEl.querySelector('.pullUpIcon').classList.remove('loading-show');
                        setTimeout(function () {
                            myScroll.refresh()
                        }, 20)
                    });


                }
            }

        }
    });
}

